package net.bw.realtime.jtp.dws.trade.job;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.TableEnvironment;

/*
 * @ Author：liuyawei
 * @ Date：2025-06-03
 */
public class JtpOrderProvinceAggregateDwsJob {

    public static void main(String[] args) {
        
        // 1.创建表环境
        TableEnvironment tabEnv = getTableEnv();

        // 2.创建输入表
        createInputTable(tabEnv);

        // 3.处理数据
        Table reportTable = handle(tabEnv);

        // 4.创建输出表
        createOutputTable(tabEnv);

        // 5.保存数据
        saveToDoris(tabEnv, reportTable);

    }

    private static void saveToDoris(TableEnvironment tabEnv, Table reportTable) {

        // 注册临时视图
        tabEnv.createTemporaryView("report_table", reportTable);

        // 执行插入语句
        tabEnv.executeSql(
                "INSERT INTO dws_trade_province_order_report_doris_sink \n" +
                        "SELECT \n" +
                        "   SUBSTRING(CAST(start_time AS STRING), 0, 19) AS start_time, \n" +
                        "   SUBSTRING(CAST(end_time AS STRING), 0, 19)   AS start_time, \n" +
                        "   SUBSTRING(CAST(start_time AS STRING), 0, 10) AS cur_date, \n" +
                        "   province_id, province_name, region_id, region_name, \n" +
                        "   order_count, order_amount, activity_amount, coupon_amount \n" +
                        "FROM report_table"
        );

    }

    // 创建输出表
    private static void createOutputTable(TableEnvironment tabEnv) {

        // 创建输出表映射Doris中的表
        tabEnv.executeSql(
                "CREATE TABLE dws_trade_province_order_report_doris_sink(\n" +
                        "   `start_time`       STRING,\n" +
                        "   `end_time`         STRING,\n" +
                        "   `cur_date`         STRING,\n" +
                        "   `province_id`      STRING,\n" +
                        "   `province_name`    STRING,\n" +
                        "   `region_id`        STRING,\n" +
                        "   `region_name`      STRING,\n" +
                        "   `order_count`      BIGINT,\n" +
                        "   `order_amount`     DECIMAL(16, 2),\n" +
                        "   `activity_amount`  DECIMAL(16, 2),\n" +
                        "   `coupon_amount`    DECIMAL(16, 2)\n" +
                        ") WITH (\n" +
                        "    'connector' = 'doris',\n" +
                        "    'fenodes' = 'node102:8030',\n" +
                        "    'table.identifier' = 'jtp_mall_report.dws_trade_province_order_report',\n" +
                        "    'username' = 'root',\n" +
                        "    'password' = '123456',\n" +
                        "    'sink.batch.interval' = '10s',\n" +
                        "    'sink.max-retries' = '3',\n" +
                        "    'sink.batch.size' = '1000'\n" +
                        ")"
        );

    }

    // 处理数据
    private static Table handle(TableEnvironment tabEnv) {

        // 使用 SQL 查询对 Kafka 输入表 dwd_order_detail_kafka_source 进行滚动窗口聚合操作
        Table result_table = tabEnv.sqlQuery(
                "SELECT\n" +
                        "    DATE_FORMAT(window_start, 'yyyy-MM-dd HH:mm:ss') AS start_time\n" +
                        "    , DATE_FORMAT(window_end, 'yyyy-MM-dd HH:mm:ss') AS end_time\n" +
                        "    , province_id\n" +
                        "    , count(DISTINCT order_id) AS order_count\n" +
                        "    , sum(cast(split_total_amount AS DECIMAL(10, 2))) AS order_amount\n" +
                        "    , sum(cast(if(split_activity_amount IS NULL, '0', split_activity_amount) AS DECIMAL(10, 2)))  AS activity_amount\n" +
                        "    , sum(cast(if(split_coupon_amount IS NULL, '0', split_coupon_amount) AS DECIMAL(10, 2)))  AS coupon_amount\n" +
                        "    , PROCTIME() AS proc_time\n" +
                        "FROM TABLE (\n" +
                        "    TUMBLE(TABLE dwd_order_detail_kafka_source, DESCRIPTOR(create_time), INTERVAL '1' MINUTES)\n" +
                        ")\n" +
                        "WHERE user_id IS NOT NULL AND source_type_name IS NOT NULL\n" +
                        "GROUP BY window_start, window_end, province_id"
        );

        // 创建视图表
        tabEnv.createTemporaryView("result_table", result_table);

        // 创建维度表 dim_base_province_hbase_source
        tabEnv.executeSql(
    "CREATE TABLE dim_base_province_hbase_source (\n" +
            "    row_key STRING,\n" +
            "    info ROW<area_code STRING, id STRING, iso_3166_2 STRING, iso_code STRING, name STRING, region_id STRING>,\n" +
            "    PRIMARY KEY (row_key) NOT ENFORCED\n" +
            ") WITH (\n" +
            "    'connector' = 'hbase-2.2',\n" +
            "    'table-name' = 'dim_base_province',\n" +
            "    'zookeeper.quorum' = 'node101:2181,node102:2181,node103:2181',\n" +
            "    'lookup.async' = 'true',\n" +
            "    'lookup.cache.max-rows' = '34',\n" +
            "    'lookup.cache.ttl' = '1 hour'\n" +
            ")"
        );

        // 创建维度表 dim_base_region_hbase_source
        tabEnv.executeSql(
   "CREATE TABLE dim_base_region_hbase_source (\n" +
            "    row_key STRING,\n" +
            "    info ROW<id STRING, region_name STRING>,\n" +
            "    PRIMARY KEY (row_key) NOT ENFORCED\n" +
            ") WITH (\n" +
            "    'connector' = 'hbase-2.2',\n" +
            "    'table-name' = 'dim_base_region',\n" +
            "    'zookeeper.quorum' = 'node101:2181,node102:2181,node103:2181',\n" +
            "    'lookup.async' = 'true',\n" +
            "    'lookup.cache.max-rows' = '34',\n" +
            "    'lookup.cache.ttl' = '1 hour'\n" +
            ")"
        );

        // 关联省份以及区域维度表
        Table report_table = tabEnv.sqlQuery(
                "SELECT\n" +
                        "    t1.start_time,\n" +
                        "    t1.end_time,\n" +
                        "    t1.province_id,\n" +
                        "    t2.name AS province_name,\n" +
                        "    t2.region_id,\n" +
                        "    t3.region_name,\n" +
                        "    t1.order_count,\n" +
                        "    t1.order_amount,\n" +
                        "    t1.activity_amount,\n" +
                        "    t1.coupon_amount,\n" +
                        "    UNIX_TIMESTAMP() * 1000 AS ts\n" +
                        "FROM result_table t1\n" +
                        "    LEFT JOIN dim_base_province_hbase_source FOR SYSTEM_TIME AS OF t1.proc_time AS t2\n" +
                        "       ON t1.province_id = t2.row_key\n" +
                        "    LEFT JOIN dim_base_region_hbase_source FOR SYSTEM_TIME AS OF t1.proc_time AS t3\n" +
                        "       ON t2.region_id = t3.row_key"
        );

        return report_table;
    }

    // 创建输入表
    private static void createInputTable(TableEnvironment tabEnv) {

        // 根据Kafka创建表
        tabEnv.executeSql(
    "CREATE TABLE dwd_order_detail_kafka_source(\n" +
            "    `id` STRING,\n" +
            "    `order_id` STRING,\n" +
            "    `user_id` STRING,\n" +
            "    `order_status` STRING,\n" +
            "    `sku_id` STRING,\n" +
            "    `sku_name` STRING,\n" +
            "    `province_id` STRING,\n" +
            "    `activity_id` STRING,\n" +
            "    `activity_rule_id` STRING,\n" +
            "    `coupon_id` STRING,\n" +
            "    `date_id` STRING,\n" +
            "    `create_time` TIMESTAMP(3),\n" +
            "    `operate_date_id` STRING,\n" +
            "    `operate_time` STRING,\n" +
            "    `source_id` STRING,\n" +
            "    `source_type` STRING,\n" +
            "    `source_type_name` STRING,\n" +
            "    `sku_num` STRING,\n" +
            "    `split_original_amount` STRING,\n" +
            "    `split_activity_amount` STRING,\n" +
            "    `split_coupon_amount` STRING,\n" +
            "    `split_total_amount` STRING,\n" +
            "    `row_op_ts` STRING,\n" +
            "    `proc_time` AS PROCTIME(),\n" +
            "    WATERMARK FOR create_time AS create_time - INTERVAL '0' MINUTE\n" +
            ") WITH (\n" +
            "    'connector' = 'kafka',\n" +
            "    'topic' = 'dwd-order-detail',\n" +
            "    'properties.bootstrap.servers' = 'node101:9092,node102:9092,node103:9092',\n" +
            "    'properties.group.id' = 'gid-dws-trade-sku-order',\n" +
            "    'scan.startup.mode' = 'earliest-offset',\n" +
            "    'format' = 'json',\n" +
            "    'json.fail-on-missing-field' = 'false',\n" +
            "    'json.ignore-parse-errors' = 'true'\n" +
            ")"
        );

    }

    // 创建表环境
    private static TableEnvironment getTableEnv() {

        // 1环境属性设置
        EnvironmentSettings settings = EnvironmentSettings.newInstance()
                .useBlinkPlanner()
                .inStreamingMode()
                .build();

        // 2配置属性设置
        TableEnvironment tabEnv = TableEnvironment.create(settings) ;

        Configuration conf = tabEnv.getConfig().getConfiguration();
        conf.setString("table.local-time-zone", "Asia/Shanghai");
        conf.setString("table.exec.resource.default-parallelism", "1");
        conf.setString("table.exec.state.ttl", "5 s");

        return tabEnv;
    }

}
